package com.wwy;

import com.wwy.domain.LabelEntity;
import com.wwy.domain.TypeEntity;
import com.wwy.service.LableSaveService;
import org.springframework.context.ApplicationContext;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: wwy
 * @Date: 2020/8/13 13:11
 */
public class LabelDialog extends JDialog implements ActionListener, ListSelectionListener {
    public static ApplicationContext applicationContext;
    public static List<LabelEntity> labelEntityList = new ArrayList<>();
    JLabel lableTitle;
    JLabel subScribeTitle;
    JTextField lableText;
    JTextField subScribeText;
    JList jList;
    JButton quitButton;
    JButton saveButton;
    JComboBox comboBox;
    public LabelDialog(JFrame F,String s){
        super(F,s,true);//模态
        this.setBounds(200, 200, 700, 540);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3,1));
        panel.setSize(700,540);


        JPanel topPanel = new JPanel();
        topPanel.setVisible(true);
        topPanel.setLayout(new FlowLayout());
        panel.add(topPanel);
        lableTitle = new JLabel("标签名称");
        //jList = new JList(getTypeArray(TypeDialog.typeEntityList));
        comboBox = new JComboBox();
        for (TypeEntity typeEntity : TypeDialog.typeEntityList) {
            comboBox.addItem(typeEntity.getTypeName());
            comboBox.setActionCommand(typeEntity.getId());
        }
        comboBox.addActionListener(this);

        topPanel.add(lableTitle);
        topPanel.add(comboBox);
        JPanel top1Panel = new JPanel();
        top1Panel.setVisible(true);
        top1Panel.setLayout(new FlowLayout());
        subScribeTitle = new JLabel("主题名称");
        subScribeText = new JTextField(30);
        subScribeText.setEditable(true);
        top1Panel.add(subScribeTitle);
        top1Panel.add(subScribeText);
        panel.add(top1Panel);

        JPanel top2Panel = new JPanel();
        top2Panel.setVisible(true);
        top2Panel.setLayout(new FlowLayout());
        quitButton = new JButton("取消");
        quitButton.setActionCommand("quit");
        quitButton.addActionListener(this);
        saveButton = new JButton("确定");
        saveButton.setActionCommand("save");
        saveButton.addActionListener(this);
        top2Panel.add(quitButton);
        top2Panel.add(saveButton);
        panel.add(top2Panel);
        panel.setVisible(true);
        setModal(false);
        this.setContentPane(panel);
        this.pack();
    }

    public String[] getTypeArray(List<TypeEntity> typeEntityList){
        String[] strArray = new String[typeEntityList.size()];
        for (int i = 0; i < typeEntityList.size(); i++) {
            TypeEntity typeEntity = typeEntityList.get(i);
            strArray[i] = typeEntity.getTypeName();
        }
        return strArray;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println(e.getActionCommand());
        switch (e.getActionCommand()){
            case "quit":
                System.out.println("执行退出");
                break;
            case "save":
                System.out.println("执行保存");
                //调用新增代码
                LableSaveService lableSaveService = applicationContext.getBean(LableSaveService.class);
                LabelEntity labelEntity = new LabelEntity();
                int selectedIndex = comboBox.getSelectedIndex();
                for(int i=0;i<TypeDialog.typeEntityList.size();i++){
                    if(selectedIndex == i){
                        labelEntity.setLableId(TypeDialog.typeEntityList.get(i).getId());
                        break;
                    }
                }

                labelEntity.setSubScribeName(subScribeText.getText());
                labelEntityList.add(labelEntity);
                lableSaveService.labelSave(labelEntityList);
                dispose();
                break;
        }
    }

    @Override
    public void valueChanged(ListSelectionEvent e) {
        System.out.println("下拉菜单执行");
    }
}

